package com.buyantech.smweb.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;
import javacommon.util.DateTimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.Ad;
import com.buyantech.smweb.model.Black;
import com.buyantech.smweb.model.CreditsExchange;
import com.buyantech.smweb.model.Ip;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.AdManager;
import com.buyantech.smweb.service.BlackManager;
import com.buyantech.smweb.service.CreditsExchangeManager;
import com.buyantech.smweb.service.IpManager;
import com.buyantech.smweb.service.UserManager;
import com.buyantech.smweb.vo.CreditsExchangeDto;
import com.buyantech.smweb.vo.query.AdQuery;
import com.buyantech.smweb.vo.query.BlackQuery;
import com.buyantech.smweb.vo.query.CreditsExchangeQuery;
import com.buyantech.smweb.vo.query.IpQuery;
import com.google.gson.Gson;

/**
 * 积分兑换记录
 * 
 * @version 1.0
 * @date 2012-05-10
 */
@Controller
public class AdController extends BaseSpringController {
	private AdManager adManager;


	public void setAdManager(
			AdManager adManager) {
		this.adManager = adManager;
	}

	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}

	/**
	 * 执行搜索
	 **/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, AdQuery query) {

		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile = request.getParameter("sortField");
		if (sortfile != null && !sortfile.equals(""))
			query.setSortColumns(request.getParameter("sortField") + " "
					+ request.getParameter("sortOrder"));

		Page page = this.adManager.searchPage(query);
		modelMap.put("data", page.getResult());
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

	/**
	 * 
	 **/
	public ModelAndView addAd(HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {
		try {
			String json = request.getParameter("submitData");
			Gson gson = new Gson();
			Map<String, String> map = new HashMap<String, String>();
			map = gson.fromJson(json, Map.class);
			String adId = (String) map.get("adId");
			String appName= (String) map.get("appName");
			String remark= (String) map.get("remark");
			Ad mAd=new Ad();
			mAd.setAdId(adId);
			mAd.setAppName(appName);
			mAd.setRemark(remark);
			adManager.save(mAd);
		} catch (Exception e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);

		return null;
	}
	
	/**
	 * 删除ad
	 **/
	public ModelAndView deleteIds(HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {
		try {
			String id = request.getParameter("ids");
			String[] ids = id.split(",");
			for (int i = 0; i < ids.length; i++) {
				 adManager.remove("DELETE FROM  Ad where id="+ids[i]);//切记hql
			}
		} catch (Exception e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);
		return null;
	}


}
